/*replication code to analyzed data for "Endless Nightmare" Torture and Inhuman Treatment in Solitary Confinement in U.S. Immigration Detention (02/06/2024).
Uses SRMS data on solitary confinement in the U.S. immigration detention facilities from 2018 to 2023, obtained by the Harvard Immigration and Refugee Clinical Program
Stata version 17*/

**********ANALYZE DATA***********

****PAGE 10********************************
*FOIA SRMS data and ICE quarterly inconsistencies
*endnotes 62-65
use srms_data.dta, clear
keep if DetaineeRequest=="Facility-Initiated" // 2978 dropped
keep if inrange(PlacementDate, td(01oct2021), td(30jun2023)) // 7421 dropped
gen vuln_bin=1 if strpos(lower(PlacementReason), "medical")>0 | strpos(lower(PlacementReason), "mental")>0 | strpos(lower(PlacementReason), "lgbt")>0 | strpos(lower(PlacementReason), "victim")>0 | strpos(lower(PlacementReason), "hunger")>0  | strpos(lower(PlacementReason), "suicide")>0  | Mental_bin==1 | SeriousMed=="Yes" | SeriousD=="Yes" | Suicide=="Suicide Risk" //variable indicating if the detainee is considered vulnerable

*create quarterly statistics for placement numbers and length for vulnerable populations
collapse (sum) vuln (mean) Length if vuln==1, by(PlacementQ)
rename vuln_bin Placement_FOIA
label var Placement_FOIA "SRMS FOIA Placements"
rename Length Length_FOIA
label var Length_FOIA "SRMS FOIA Length
gen PlacementFQ=PlacementQ[_n+1] // converting dates to fiscal quarters
replace PlacementFQ=254 if PlacementFQ==.
format %tq PlacementFQ
drop PlacementQ
sort PlacementFQ
save 2022_2023_FOIA_confinement_vulnerable, replace

*merge with ICE quarterly data
import excel "C:\Users\aavedian\Desktop\backup\backup01232024\data_merge\HarvardDataverse\2022_2023_ICE_quarterly_confinement_vulnerable.xlsx", sheet("combined22_23") firstrow clear
gen PlacementFQ = quarterly(PlacementQ, "YQ")
format PlacementFQ %tq
label var Placement_ICE "ICE Quarterly Placements"
label var Length_ICE "ICE Quarterly Length"
drop PlacementQ
sort PlacementFQ
merge 1:1 PlacementFQ using 2022_2023_FOIA_confinement_vulnerable

*Graphic 2
twoway (line Placement_FOIA PlacementFQ, ytitle("") xtitle("")) (line Placement_ICE PlacementFQ), title(`"Graphic 2: Number of Solitary Confinement Placements for Immigrants With Vulnerabilities"', size(small)) xlabel(#6) graphregion(fcolor(white))

****PAGE 11********************************
*Graphic 3
twoway (line Length_FOIA PlacementFQ, ytitle("") xtitle("")) (line Length_ICE PlacementFQ), title(`"Graphic 3: Length of Solitary Confinement for Immigrants With Vulnerabilities"', size(med))  xlabel(#6) graphregion(fcolor(white))

****PAGE 14********************************
*number of placements
use srms_data.dta, clear
count // 14264 observations

*data includes placements with release dates between 04sep2018 and 12sep2023
codebook ReleaseD if ReleaseD!=. 	// release dates are 04sep2018-13sep2023
codebook PlacementD if ReleaseD!=. 	// placement dates are 29aug2017, 1sep2023

*length of solitary confinement 
sum Length, det 					// mean is 26.61, median is 15, n=14109 
sum Length if Length<15  			// n=6692 
sum Length if Length>15  			// n=6822
sum Length if Length==15 			// n=595
tab Length 							// 52.57% were in confinement 15 days or longer

*number of placements >=3 and 12 months
bysort Mental_bin: sum Length if Length>=90 // 682 placements over 90 days, 193 with mental illness
bysort Mental_bin: sum Length if Length>365 // 42 placements over 1 year, 10 with mental illness

*facilities with longest solitary confinement placements
gsort -Length
list Length Facility in 1/10

*facilities with facility-initiated longest solitary confinement placements (endnote 78)
gsort   -Length 
list Length Facility in 1/50 if DetaineeRequestSegregation=="Facility-Initiated"

*number of facilities
codebook Facility

*number of placements per year
tab PlacementY // data starts September 2018, in 2019 n=3022, in 2020 n=3260, in 2021 n=2096, in 2022 n=2494 and by mid-September 2023 n=2301 

*number of placements per month
bysort PlacementY PlacementM: count

*average length by year, using Release dates, to include everyone that was in placement each year
bysort ReleaseY: sum Length

****PAGE 15********************************
*Graphic 4: Number of Immigrants Held in Solitary Confinement out of Total Population in Detention
import excel "C:\Users\aavedian\Desktop\backup\backup01232024\data_merge\HarvardDataverse\2019_2023_daily_detention_confinement", sheet("combined") firstrow clear
label var confinement_prop "Number in confinement per 10,000 detainees"
twoway (line confinement_prop YearMonth, ytitle("") xtitle("")), xlabel(#7) ylabel(0(50)200) title(`"Graphic 4: Number of Immigrants Held in Solitary Confinement out of Total Population in Detention"', size(small)) subtitle(`"Per 10,000 Detained Immigrants"', size(vsmall))

*Graphic 5: figure is from DMC FY23 Q3 Segregation Metrics.pdf

****PAGE 16********************************
*Graphic 6
use srms_data.dta, clear
bysort ReleaseY: egen yearly_average_length = mean(Length)
label var yearly_average_length  "Average Number of Days in Solitary Confinement"
twoway (line yearly_average_length ReleaseY if ReleaseY>=2019, ytitle("")), yline(15, lpattern(solid)) ylabel(0(5)35) ymtick(, labels) title(`"Graphic 6: Average Number of Days in Solitary Confinement"') legend(on)

*transgender statistic
sum Length if PlacementR=="Protective Custody: Lesbian, Gay, Bisexual, Transgender (LGBT)", det // n=62, mean=57.24, median=34

****PAGE 17********************************
*mental illness
tab Mental_bin
tab Mental_bin, mi // 38.40% missing
tab ReleaseY Mental_bin
tab ReleaseY MentalI

*serious mental illness
tab ReleaseY SeriousMental, ro

*length of solitary confinement for people with mental and serious mental illnesses
bysort MentalI: sum Length

*facilities with highest mean facility-initiated length for people with mental illnesses
egen mean=mean(Length) if Mental_bin==1 & DetaineeRequestSegregation=="Facility-Initiated", by(Facility) 
egen group=group(mean Facility)
replace group = -group
labmask group, values(Facility)
label var group "`: var label Facility'"
tabstat Length, by(group) s(mean) 

*****PAGE 18********************************
*Graphic 7:
gen Mental_graph=1 if Mental_bin==1
gen NonMental_graph=1 if Mental_bin==0
label var Mental_graph "Mental Illness"
label var NonMental_graph "No Mental Illness"

graph bar (count) NonMental_g (count) Mental_g if ReleaseY>2018 , over(ReleaseY) percentages stack bar(1, fcolor(black)) bar(2, fcolor(maroon)) blabel(bar, size(large) position(inside) format(%9.1g) justification(center)) ytitle(`"Percent"') title(`"Graphic 7: Percent in Solitary Confinement with Mental Illness"', size(med)) legend(on order(1 "`: var label NonMental_g'" 2 "`: var label Mental_g'")) scheme(s2color8) 